import VendorStore from './VendorStore'

class GoodStore {
  /** 原始商品数据对象 就是 sorted 数组的每一项 */
  good: ISkuObj
  /** 上级 vendorStore 对象 */
  vendorStore: VendorStore
  /** 商品是否选中 */
  isChecked: boolean = false
  /** */
  SType?: number
  /** 是否套装 */
  isSuit: boolean = false
  /** 是否非普通单品*/
  isNonSingle?: boolean
  /** 新满赠标识 */
  isNewManZeng: boolean = false
  /** */
  highestItem?: ISkuItemCombine | ISkuObj
  /** 是否是合约机 */
  isHYJ?: boolean
  /** 是否是卡 */
  isK?: boolean

  constructor(good: ISkuObj, vendorStore: VendorStore) {
    this.good = good
    this.vendorStore = vendorStore
    this.setIsChecked()
    this.setIsNewManZeng()
    this.setIsHYJ()
  }

  private setIsChecked() {
    const item = this.good.item || {}
    this.isChecked = item.isNoCheck === 0 && item.CheckType === 1
  }

  private setIsNewManZeng() {
    const good = this.good
    this.isNewManZeng = !!(
      good.item?.newManZengPromotionInfo ||
      good.item?.items?.map((m) => m?.item?.newManZengPromotionInfo)?.filter(Boolean)?.length ||
      good.item?.items
        ?.map((n) => n.item?.items?.map((m) => m?.item?.newManZengPromotionInfo))
        ?.flat()
        ?.filter(Boolean)?.length
    )
  }

  private setIsHYJ() {
    const item: any = this.good.item
    const specialId: string = item?.specialId ?? '0'
    // 通过 specialId 判断，第26位为真是号卡，第27位为真是合约机
    this.isHYJ = !!((Number(specialId) >> 27) & 1)
    this.isK = !!((Number(specialId) >> 26) & 1)
  }
}

export default GoodStore
